package com.sidianzhong.sdz.service.impl;
import com.github.pagehelper.PageHelper;
import com.sidianzhong.sdz.mapper.*;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by hxgqh on 2016/1/7.
*/
@Service
@Transactional
public class UserWalletServiceImpl implements UserWalletService{
    protected static final Logger LOG = LoggerFactory.getLogger(UserWalletServiceImpl.class);
    // 将所有的modelMapper注入
        @Autowired
    private UserWalletMapper userWalletMapper;

    @Override
    public UserWallet create(UserWallet item) {
         userWalletMapper.insert(item);
         return item;
    }

    @Override
    public int delete(Integer id) {
        return userWalletMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(UserWallet item) {
        return userWalletMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    public UserWallet get(Integer id) {
        return userWalletMapper.selectByPrimaryKey(id);
    }
    @Override
    public PageInfo<UserWallet> getListWithPaging(Integer pageNum, Integer pageSize,
                                                  String sortItem, String sortOrder,UserWallet item){
       UserWalletExample  example = new UserWalletExample();
       UserWalletExample.Criteria criteria = example.createCriteria();
       if(item.getAddress() != null ){
            criteria.andAddressEqualTo(item.getAddress());
        }
        if(item.getPrivatekey() != null ){
            criteria.andPrivatekeyEqualTo(item.getPrivatekey());
        }
        if(item.getUserId() != null ){
            criteria.andUserIdEqualTo(item.getUserId());
        }
        if(item.getAmount() != null ){
            criteria.andAmountEqualTo(item.getAmount());
        }
        if(item.getStatus() != null ){
            criteria.andStatusEqualTo(item.getStatus());
        }
        if(item.getCoinId() != null ){
            criteria.andCoinIdEqualTo(item.getCoinId());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLastUpdateTime() != null ){
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<UserWallet> list = this.userWalletMapper.selectByExample(example);
       PageInfo result = new PageInfo(list);
       return result;
    }

    @Override
    public List<UserWallet> getList(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,UserWallet item){
       UserWalletExample  example = new UserWalletExample();
       UserWalletExample.Criteria criteria = example.createCriteria();
       if(item.getAddress() != null ){
            criteria.andAddressEqualTo(item.getAddress());
        }
        if(item.getPrivatekey() != null ){
            criteria.andPrivatekeyEqualTo(item.getPrivatekey());
        }
        if(item.getUserId() != null ){
            criteria.andUserIdEqualTo(item.getUserId());
        }
        if(item.getAmount() != null ){
            criteria.andAmountEqualTo(item.getAmount());
        }
        if(item.getStatus() != null ){
            criteria.andStatusEqualTo(item.getStatus());
        }
        if(item.getCoinId() != null ){
            criteria.andCoinIdEqualTo(item.getCoinId());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLastUpdateTime() != null ){
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<UserWallet> list = this.userWalletMapper.selectByExample(example);
       return list;
    }

     @Override
     public PageInfo<JSONObject> getListWithObject(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,UserWallet item){
        UserWalletExample  example = new UserWalletExample();
        UserWalletExample.Criteria criteria = example.createCriteria();
        if(item.getAddress() != null ){
            criteria.andAddressEqualTo(item.getAddress());
        }
        if(item.getPrivatekey() != null ){
            criteria.andPrivatekeyEqualTo(item.getPrivatekey());
        }
        if(item.getUserId() != null ){
            criteria.andUserIdEqualTo(item.getUserId());
        }
        if(item.getAmount() != null ){
            criteria.andAmountEqualTo(item.getAmount());
        }
        if(item.getStatus() != null ){
            criteria.andStatusEqualTo(item.getStatus());
        }
        if(item.getCoinId() != null ){
            criteria.andCoinIdEqualTo(item.getCoinId());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLastUpdateTime() != null ){
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
        example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
        PageHelper.startPage(pageNum, pageSize);
        List<UserWallet> list = this.userWalletMapper.selectByExample(example);
        List<JSONObject> lists =  list.stream().map(userWallet -> JSONObject.parseObject(JSONObject.toJSONString(userWallet))).collect(Collectors.toList());
        PageInfo result = new PageInfo(lists);
        return result;
        }

    @Override
    public UserWallet getByUserId(Integer userId, Integer coinId) {

        UserWalletExample  example = new UserWalletExample();
        UserWalletExample.Criteria criteria = example.createCriteria();

        if(userId != null ){
            criteria.andUserIdEqualTo(userId);
        }
        if(coinId != null ){
            criteria.andCoinIdEqualTo(coinId);
        }
        List< UserWallet> list = this.userWalletMapper.selectByExample(example);
        if (list.size()>0){
            UserWallet userWallet = list.get(0);
            return userWallet;
        }
        return null;
    }

}
